package com.zork.cmdb.webapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zork.cmdb.webapi.domain.dbo.RelationshipTypeDO;
import com.zork.cmdb.webapi.domain.dto.RelationshipTypeDTO;
import com.zork.cmdb.webapi.domain.vo.RelationshipTypeVO;
import com.zork.cmdb.webapi.mapper.cmdb.RelationshipTypeMapper;
import com.zork.cmdb.webapi.service.RelationshipTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author chenjh
 * @date 2018/7/3
 * @description
 */
@Service
public class RelationshipTypeServiceImpl implements RelationshipTypeService {

    @Autowired
    private RelationshipTypeMapper relationshipTypeMapper;

    @Override
    public PageInfo<Object> listRelationshipType(Integer pageNum, Integer pageSize) {
        PageInfo<Object> relationshipTypeObjectList = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> relationshipTypeMapper.listRelationshipType());
        List<Object> objectList = relationshipTypeObjectList.getList();
        List<RelationshipTypeDO> relationshipTypeDOList = getRelationshipTypeDOList(objectList);
        List<RelationshipTypeDTO> relationshipTypeDTOList = com.zork.cmdb.webapi.domain.mapper.RelationshipTypeMapper.MAPPER.getDTOList(relationshipTypeDOList);
        List<RelationshipTypeVO> relationshipTypeVOList = com.zork.cmdb.webapi.domain.mapper.RelationshipTypeMapper.MAPPER.getVOList(relationshipTypeDTOList);
        relationshipTypeObjectList.setList(Collections.singletonList(relationshipTypeVOList));
        return relationshipTypeObjectList;
    }

    private List<RelationshipTypeDO> getRelationshipTypeDOList(List<Object> objectList) {
        List<RelationshipTypeDO> relationshipTypeDOList = new ArrayList<>();
        for(Object object : objectList) {
            if (object instanceof RelationshipTypeDO) {
                RelationshipTypeDO relationshipTypeDO = (RelationshipTypeDO) object;
                relationshipTypeDOList.add(relationshipTypeDO);
            }
        }
        return relationshipTypeDOList;
    }
}
